﻿using cap01.Application.Home.Services;
using cap01.Common.Rabbitmq;
using cap01.Common.Redis;
using cap01.Common.Util;
using Microsoft.AspNetCore.Authorization;

namespace cap01.Application.Home;

/// <summary>
/// 此控制器用于案例
/// </summary>
[ApiRoute.App]
public class HomeController(ILogger<HomeController> logger, HomeService service)
{
    private readonly ILogger<HomeController> _logger = logger;
    private readonly HomeService service = service;
    private static RedisPubSub _redisPubSub = new(RedisNotify.Notify.ToString());
    static HomeController()
    {
        _redisPubSub.Subscribe(msg =>
        {
            LoggerHelper.Info("redis msg: " + msg);
        });
        MQClients.SimpleNotify.Subscribe(10, msg =>
        {
            LoggerHelper.Info("MQ简单消息: " + msg);
            return Task.CompletedTask;
        });
        MQClients.FanoutNotify.Subscribe(10, msg =>
        {
            LoggerHelper.Info("MQ广播消息: " + msg);
            return Task.CompletedTask;
        });
    }

    /// <summary>
    /// 获取当前时间
    /// </summary>
    /// <returns></returns>
    [HttpGet("[action]")]
    public string GetTime()
    {
        var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        _logger.LogInformation("当前时间：" + time);
        return time;
    }

    /// <summary>
    /// 获取当前时间
    /// </summary>
    /// <returns></returns>
    [HttpGet("[action]")]
    [AllowAnonymous]
    public string GetTime2()
    {
        var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        _logger.LogInformation("当前时间：" + time);
        return time;
    }

    /// <summary>
    /// 创建订单
    /// </summary>
    /// <returns></returns>
    [HttpPost("[action]")]
    public OrderDto CreateOrder(OrderDto dto)
    {
        var order = HomeService.CreateOrder(dto);
        return order.Adapt<OrderDto>();
    }

    /// <summary>
    /// 创建报告
    /// </summary>
    /// <returns></returns>
    [HttpPost("[action]")]
    public ReportDto CreateReport(ReportDto dto)
    {
        var report = service.CreateReport(dto);
        return report.Adapt<ReportDto>();
    }

    /// <summary>
    /// 获取报告
    /// </summary>
    /// <returns></returns>
    [HttpGet("[action]")]
    public List<ReportDto> GetReport()
    {
        var reports = HomeService.GetReports();
        return reports.Adapt<List<ReportDto>>();
    }

    /// <summary>
    /// 发布Redis消息
    /// </summary>
    [HttpGet("[action]")]
    public void Redis()
    {
        _redisPubSub.Publish(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    }

    /// <summary>
    /// 发布mq简单消息
    /// </summary>
    [HttpGet("[action]")]
    public void MqSimple()
    {
        MQClients.SimpleNotify.Publish(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    }

    /// <summary>
    /// 发布mq广播消息
    /// </summary>
    [HttpGet("[action]")]
    public void MqBroadcost()
    {
        MQClients.FanoutNotify.Publish(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}
